package net.hn.hnms.biz.emgcy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.emgcy.domain.EmergencyTrainingPo;
import net.hn.hnms.biz.emgcy.mapper.EmergencyTrainingMapper;
import net.hn.hnms.biz.emgcy.service.EmergencyTrainingService;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.file.api.RemoteFileService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: EmergencyTrainingServiceImpl
 * @Description 培训考评
 * @Author 王龙
 * @Date 2022-10-14
 * @Version 0.1
 */
@Service
public class EmergencyTrainingServiceImpl implements EmergencyTrainingService {
    @Resource
    private EmergencyTrainingMapper emergencyTrainingMapper;

    @Resource
    private RemoteFileService remoteFileService;
    /**
     * 培训考评-添加
     * @param emergencyTrainingPo
     * @return
     */
    @Override
    public Boolean add(EmergencyTrainingPo emergencyTrainingPo) {
        int insert = emergencyTrainingMapper.insert(emergencyTrainingPo);
        if (insert > 0) {
            return true;
        }
        return false;
    }

    /**
     * 培训考评-列表
     * @return
     */
    @Override
    public ResponsePage<EmergencyTrainingPo> pageList(PageQuery pageQuery, String name,String mineCode) {
        LambdaQueryWrapper<EmergencyTrainingPo> emergencyTrainingPoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        emergencyTrainingPoLambdaQueryWrapper.like(StringUtils.isNotBlank(name), EmergencyTrainingPo::getEmergencyTitle, name);
        emergencyTrainingPoLambdaQueryWrapper.eq(StringUtils.isNotBlank(mineCode), EmergencyTrainingPo::getMineCode, mineCode);
        Page<EmergencyTrainingPo> emergencyTrainingPoPage = emergencyTrainingMapper.selectPage(pageQuery.build(), emergencyTrainingPoLambdaQueryWrapper);

        if (emergencyTrainingPoPage.getRecords().isEmpty()) {
            return new ResponsePage<>(new ArrayList(), 0);
        }
        return new ResponsePage<>(emergencyTrainingPoPage.getRecords(), emergencyTrainingPoPage.getTotal());
    }

    /**
     * 培训考评-根据ID删除
     * @param id
     * @return
     */
    @Override
    public Boolean delete(String id) {
        EmergencyTrainingPo emergencyTrainingPo = emergencyTrainingMapper.selectById(id);
        if (emergencyTrainingPo == null) {
            throw new HuaNengException(Constants.FAIL, "未查询到该id" + id);
        }
        Long[] ossIds = new Long[]{Long.parseLong(emergencyTrainingPo.getEmergencyCoverId()), Long.parseLong(emergencyTrainingPo.getEmergencyContentId())};
         remoteFileService.remove(ossIds);
        int i = emergencyTrainingMapper.deleteById(id);
        if (i>0)
            return true;
        return false;
    }

    /**
     * 培训考评-根据ID查看详情
     * @param id
     * @return
     */
    @Override
    public EmergencyTrainingPo getByID(String id) {
        EmergencyTrainingPo emergencyTrainingPo = emergencyTrainingMapper.selectById(id);
        if (emergencyTrainingPo == null) {
            throw new HuaNengException(Constants.FAIL, "未查询到该ID" + id);
        }
        return emergencyTrainingPo;
    }

    /**
     * 培训考评-根据ID批量删除
     * @param idList
     * @return
     */
    @Override
    public Boolean deleteAll(List<String> idList) {

        String falseString  = new String();
        int count = 0;
        for (String s : idList) {
            EmergencyTrainingPo emergencyTrainingPo = emergencyTrainingMapper.selectById(s);
            if (emergencyTrainingPo == null) {
                falseString += (","+s);
            }
            Long[] ossIds = new Long[]{Long.parseLong(emergencyTrainingPo.getEmergencyCoverId()), Long.parseLong(emergencyTrainingPo.getEmergencyContentId())};
            remoteFileService.remove(ossIds);
            count += emergencyTrainingMapper.deleteById(s);
        }

        if (count != idList.size()) {
            throw new HuaNengException(Constants.FAIL, "删除失败ID" + falseString);
        }
        return true;
    }

}
